package com.ruoyi.web.controller.baodao.service;

import com.ruoyi.baodao.entity.JDTJ;
import com.ruoyi.baodao.entity.JDTJExample;
import com.ruoyi.baodao.mapper.BDMapper;
import com.ruoyi.baodao.mapper.JDTJMapper;
import com.ruoyi.baodao.vo.JDTJFileVO;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
@DataSource(value = DataSourceType.SLAVE)
public class JDTJService {
    @Autowired
    JDTJMapper jdtjMapper;
    @Autowired
    BDMapper bdMapper;

    // 导入京东推荐数据
    @Transactional
    public Boolean importData(List<JDTJ> list, String fileName, String shopId){


        // 判断文件名是否重复
        List<JDTJFileVO> jdtjs = bdMapper.queryJDTJFile();
        for (JDTJFileVO file:
             jdtjs) {
            if(fileName.equals(file.getFileName()))
            {
                throw new BusinessException("文件名重复");
            }
        }



        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new BusinessException("导入数据不能为空！");
        }
        else {
            Date date = new Date();
            for (JDTJ jdtj:
                 list) {
                jdtj.setFileName(fileName);
                jdtj.setCreateTime(date);
                jdtj.setStatus("1");
                jdtj.setShopId(shopId);
               jdtjMapper.insert(jdtj);
            }
        }
        return true;
    }

    // 数据列表
    public  List<JDTJ> list(){
        return  jdtjMapper.selectByExample(new JDTJExample());
    }


    // 京东推荐 文件上传 页面
    public  List<JDTJFileVO> queryJDTJFileList(){
      // return   jdtjMapper.queryJDTJFile();
       return   bdMapper.queryJDTJFile();
    }

    // list
    public  List<JDTJ> JDTJList(){
        return jdtjMapper.selectByExample(new JDTJExample());
    }


    // 根据文件名 停用京东推荐文件






}
